/*
 *  Copyright (C) 2011  Clint Priest
 *
 *  This file is part of pklib.
 *
 *  pklib is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  pklib is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with pklib.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Portions borrowed or re-written from prototype
 */

var ElementHighlighter = Class.create({
	initialize: function(BackgroundColor, Opacity) {
		this.BackgroundColor = BackgroundColor || '#FF0000';
		this.Opacity = Opacity || 0.3;
		this.Elements = [ ];
	},
	/* Hide highlights */
	Hide: function() { 
		this.Elements.forEach( function(elem) {
			elem.style.display = 'none';
		} );
	},
	/* Show highlights */
	Show: function() { 
		this.Elements.forEach( function(elem) {
			elem.style.display = '';
		} );
	},
	/* Remove highlights */
	Clear: function() {
		this.Elements.forEach( function(elem) {
			elem.remove();
		} );
		this.Elements = [ ];
	},
	
	CreateHighlight: function(Rect) {
		var elem = $($doc().createElement('div'));
		elem.ApplyStyle( {
			backgroundColor		: this.BackgroundColor,
			opacity				: this.Opacity,
			top					: Rect.top + 'px',
			left				: Rect.left + 'px',
			height				: (Rect.bottom - Rect.top) + 'px',
			width				: (Rect.right - Rect.left) + 'px',
			position			: 'absolute',
			zIndex				: 200,
			pointerEvents		: 'none',
		} );
		$doc().body.appendChild(elem);
		this.Elements.push(elem);
	},
	
	HighlightElements: function(Elements) {
		this.Clear();
		Elements.invoke( 'GetOffsetElementRects' )
			.flatten()
			.forEach(function(Rect) {
				this.CreateHighlight(Rect);
			}, this);
	},
} );